package com.example.springbook.controller;

import com.example.springbook.constant.Constants;
import com.example.springbook.model.Result;
import com.example.springbook.model.UserInfo;
import com.example.springbook.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class userController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login", produces = "application/json")
    public Result login(String userName, String password, HttpSession session) {
        //1.校验参数
        //2.验证密码是否正确
        //3.返回响应结果
        //System.out.println(userName + " " + password);
        log.info("用户尝试登录: name:{}, password:{}",userName,password);
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return Result.fail("用户名或密码为空");
        }
//        //理论上应该从数据库中读取, 这里不是
//        if(!"admin".equals(userName) || !"admin".equals(password)) {
//            return "密码错误";
//        }

        //根据用户名称, 去数据库查询用户信息, 如果为查询到, 说明用户不存在
        //如果查询到用户信息, 对比密码是否正确
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo == null) {
            return Result.fail("用户不存在");
        }
        //用户存在,校验密码是否正确
        if(!password.equals(userInfo.getPassword())) {
            return Result.fail("密码错误");
        }

        //正确的情况
        session.setAttribute(Constants.USER_SESSION_KEY,userInfo);
        return Result.success("");
    }
}
